package com.example.zce.library.data.sys.service;

import com.example.zce.library.common.bean.ResultTableBean;
import com.example.zce.library.common.utils.UUIDUtils;
import com.example.zce.library.data.sys.dao.BackRoleMapper;
import com.example.zce.library.data.sys.dao.BackUserMapper;
import com.example.zce.library.data.sys.entity.BackRole;
import com.example.zce.library.data.sys.entity.BackUser;
import com.example.zce.library.data.sys.model.BackUserModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 描述：
 *
 * @author zengyz
 * @date 2018/7/26 23:54
 */
@Service
public class BackUserService {

    @Autowired
    private BackUserMapper backUserMapper;

    @Autowired
    private BackRoleMapper backRoleMapper;

    /**
     * 新增或修改用户
     *
     * @param backUser
     * @return
     * @throws Exception
     */
    @Transactional
    public int saveOrUpdate(BackUser backUser) throws Exception {
        if (StringUtils.isNotEmpty(backUser.getId())) {
            if (StringUtils.isNotEmpty(backUser.getPassword())){
                backUser.setPassword(DigestUtils.md5Hex(backUser.getPassword()));
            }else{
                backUser.setPassword(null);
            }
            return backUserMapper.updateByPrimaryKeySelective(backUser);
        } else {
            backUser.setId(UUIDUtils.getUUID32());
            backUser.setCreateTime(new Date());
            backUser.setPassword(DigestUtils.md5Hex(backUser.getPassword()));
            return backUserMapper.insert(backUser);
        }
    }

    /**
     * 条件查询-分页
     *
     * @param params
     * @return
     */
    public ResultTableBean<BackUserModel> findByParams(Map params) {
        PageHelper.startPage(Integer.valueOf(params.get("pageNum").toString()), Integer.valueOf(params.get("pageSize").toString()));
        List<BackUser> backUsers = backUserMapper.selectListByParams(params);
        PageInfo<BackUser> pageInfo = new PageInfo<>(backUsers);

        List<BackUserModel> backUserModels=new ArrayList<>();
        for (BackUser backUser:backUsers) {
            BackRole backRole = backRoleMapper.selectByPrimaryKey(backUser.getRoleId());
            BackUserModel init = BackUserModel.init(backUser, backRole);
            backUserModels.add(init);
        }

        ResultTableBean<BackUserModel> resultTableBean = new ResultTableBean<>(0, backUserModels, pageInfo.getTotal());
        return resultTableBean;
    }

    /**
     * 登录
     *
     * @param account
     * @return
     */
    public BackUser findByLogin(String account) {
        Map params = new HashMap();
        params.put("account", account);
        List<BackUser> backUsers = backUserMapper.selectListByParams(params);
        if (backUsers.isEmpty()) {
            return null;
        } else {
            return backUsers.get(0);
        }
    }

    public BackUser findById(String id) {
        return backUserMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int delete(String id) throws Exception{
        return backUserMapper.deleteByPrimaryKey(id);
    }
}

